package com.jswdwsx.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Slf4j
@Component
public class ImageUtils {

    private static ObjectMapper tbObjectMapper = new Jackson2ObjectMapperBuilder().build();

    public ImageUtils(){
    }

    public static String getFirstImage(String carouselImages) {
        if (!StringUtils.isEmpty(carouselImages)) {
            try {
                return tbObjectMapper.readValue(carouselImages, List.class).get(0).toString();
            } catch (Exception e) {
                log.error("详情图转换失败");
            }
        }
        return "";
    }

    public static List getImages(String carouselImages) {
        if (StringUtils.hasLength(carouselImages)) {
            try {
                return tbObjectMapper.readValue(carouselImages, List.class);
            } catch (IOException e) {
                log.error("商品轮播图格式转换失败");
            }
        }
        return Collections.EMPTY_LIST;
    }
}




